<?php


namespace app\api\service;

class AppService
{
	public static function get_feedback_classify_list($options)
	{
		$list = \think\Db::name("app_feedback_classify")->where([["appid", "=", $options["appid"]]])->order(["sort" => "desc"])->select();
		return ["code" => \app\api\controller\Code::success, "data" => ["list" => $list, "count" => count($list)]];
	}
	public static function get_feedback_data_list($options)
	{
		$page = $options["page"] ?? 1;
		$limit = $options["limit"] ?? 10;
		if ($limit == -1) {
			$limit = 1.0E+18;
		}
		$where[] = ["appid", "=", $options["appid"]];
		if (isset($options["classify_id"]) && $options["classify_id"] != "") {
			$where[] = ["classify_id", "=", $options["classify_id"]];
		}
		if (isset($options["device_code"]) && $options["device_code"] != "") {
			$where[] = ["device_code", "=", $options["device_code"]];
		}
		$list = \think\Db::name("app_feedback_list")->where($where)->order(["id" => "desc"])->page($page, $limit)->select();
		$count = \think\Db::name("app_feedback_list")->where($where)->count();
		$pages = ceil($count / $limit);
		$data = [];
		foreach ($list as $item) {
			$data[] = self::get_feedback_data_info_array($item);
		}
		return ["code" => \app\api\controller\Code::success, "data" => ["list" => $data, "pages" => $pages, "count" => $count, "page" => $page]];
	}
	public static function get_feedback_data_info($options)
	{
		$item = \think\Db::name("app_feedback_list")->where(["id" => $options["feedback_id"], "appid" => $options["appid"]])->find();
		if (empty($item)) {
			return ["code" => 10017, "msg" => "不存在该ID"];
		}
		return ["code" => \app\api\controller\Code::success, "data" => self::get_feedback_data_info_array($item)];
	}
	private static function get_feedback_data_info_array($item)
	{
		$data = $item;
		$data["classify_info"] = $item["classify_id"] === 0 ? (object) [] : \think\Db::name("app_feedback_classify")->where(["appid" => $item["appid"], "id" => $item["classify_id"]])->field("id,name,cover")->find();
		$file_list = [];
		foreach (json_decode($item["file_ids"]) as $file_id) {
			$file = \think\Db::name("public_upload_log")->where(["id" => $file_id])->find();
			if (!empty($file)) {
				if (empty($file["channel"])) {
					if ($file["type"] == 0) {
						$file["url"] = get_static_resources_url($file["uri"]);
					} elseif ($file["type"] == 1) {
						$file["url"] = get_static_resources_url($file["uri"], 1);
					} elseif ($file["type"] == 2) {
						$file["url"] = get_static_resources_url($file["uri"], 2);
					}
				}
				$file_list[] = $file;
			} else {
				$file_list[] = [];
			}
		}
		$data["file_list"] = $file_list;
		return $data;
	}
	public static function push_feedback_info($options, $mode)
	{
		$isSystemAuthUnpaid = isSystemAuthUnpaid();
		if ($isSystemAuthUnpaid && \think\Db::name("app_feedback_list")->where(["appid" => $options["appid"]])->count() >= \app\common\model\FreeQuota::AppFeedbackQuota) {
			return ["code" => \app\api\controller\Code::fail, "msg" => "当前授权为基础版,一个应用反馈数量上限为" . \app\common\model\FreeQuota::AppFeedbackQuota . \app\common\model\DefaultConfig::SystemAuthUnpaidSuffixText];
		}
		$app = \think\Db::name("app")->where(["id" => $options["appid"]])->find();
		$feedback_config = json_decode($app["feedback_config"], true);
		if (empty($feedback_config[$mode . "_feedback_config"]["enable_feedback"])) {
			return ["code" => 10267, "msg" => "未开启反馈功能"];
		}
		if (!empty($feedback_config[$mode . "_feedback_config"]["must_bring_pictures"]) && empty($options["file_ids"])) {
			return ["code" => 10263, "msg" => "提交反馈必须带上图片,请上传截图"];
		}
		if (!empty($feedback_config[$mode . "_feedback_config"]["must_bring_contact_info"]) && empty($options["contact_info"])) {
			return ["code" => 10264, "msg" => "提交反馈必须填写联系方式，请输入联系方式"];
		}
		if (!empty($feedback_config[$mode . "_feedback_config"]["limit_wordage_minimum"]) && mb_strlen($options["content"]) < $feedback_config[$mode . "_feedback_config"]["limit_wordage_minimum"]) {
			return ["code" => 10265, "msg" => "反馈内容 不能少于{$feedback_config[$mode . "_feedback_config"]["limit_wordage_minimum"]}个字"];
		}
		if (!empty($feedback_config[$mode . "_feedback_config"]["limit_wordage_maximum"]) && mb_strlen($options["content"]) > $feedback_config[$mode . "_feedback_config"]["limit_wordage_maximum"]) {
			return ["code" => 10266, "msg" => "反馈内容 最多{$feedback_config[$mode . "_feedback_config"]["limit_wordage_maximum"]}个字"];
		}
		$ip = $options["client_ip"];
		$limit_once_random_interval_minimum = $feedback_config[$mode . "_feedback_config"]["limit_once_random_interval_minimum"] ?? 0;
		$limit_once_random_interval_maximum = $feedback_config[$mode . "_feedback_config"]["limit_once_random_interval_maximum"] ?? 0;
		if ($limit_once_random_interval_minimum && $limit_once_random_interval_maximum) {
			$last_log = \think\Db::name("app_feedback_list")->where([["appid", "=", $options["appid"], ["", "exp", \think\Db::raw("device_code = '{$options["device_code"]}' or ip = '{$ip}'")]]])->order("id", "desc")->find();
			if ($last_log && time() - $last_log["create_time"] < rand($limit_once_random_interval_minimum, $limit_once_random_interval_maximum)) {
				return ["code" => 10018, "msg" => "提交反馈太频繁了，请稍后再试"];
			}
		}
		if (isset($options["classify_id"]) && $options["classify_id"] && \think\Db::name("app_feedback_classify")->where(["appid" => $options["appid"], "id" => $options["classify_id"]])->count() == 0) {
			return ["code" => 10019, "msg" => "分类不存在"];
		}
		\think\Db::startTrans();
		try {
			$carry_image = 0;
			$carry_video = 0;
			$file_ids = !empty($options["file_ids"]) ? $options["file_ids"] : [];
			if ($file_ids) {
				$file_ids = explode(",", $file_ids);
				$file_ids = array_unique($file_ids);
			}
			if (is_array($file_ids)) {
				foreach ($file_ids as $file_id) {
					$file = \think\Db::name("public_upload_log")->where(["id" => $file_id])->find();
					if (!empty($file)) {
						\think\Db::name("public_upload_log")->where(["id" => $file["id"]])->update(["overtime" => 0]);
					}
					if ($file["type"] == 0) {
						$carry_image = 1;
					}
					if ($file["type"] == 1) {
						$carry_video = 1;
					}
				}
			}
			if (count($file_ids) == 2 && $carry_video && $carry_image) {
				$carry_image = 0;
			}
			$id = \think\Db::name("app_feedback_list")->insertGetId(["appid" => $options["appid"], "title" => $options["title"] ?? "", "content" => $options["content"], "device_info" => $options["device_info"], "device_code" => $options["device_code"], "contact_info" => $options["contact_info"] ?? "", "problem_time" => isset($options["problem_time"]) && $options["problem_time"] ? is_numeric($options["problem_time"]) && strlen($options["problem_time"]) == 10 ? $options["problem_time"] : strtotime($options["problem_time"]) : "", "file_ids" => json_encode($file_ids), "create_time" => time(), "classify_id" => $options["classify_id"] ?? "0", "carry_image" => $carry_image, "carry_video" => $carry_video, "ip" => $ip]);
			\think\Db::commit();
			return ["code" => \app\api\controller\Code::success, "data" => ["feedback_id" => $id]];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\api\controller\Code::fail, "msg" => "发送失败 " . $e->getMessage()];
		}
	}
}